<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/11/6
 * Time: 16:48
 */

namespace app\adminapiv3\controller\platform_manage;

//更新
use app\adminapiv3\controller\BaseController;
use app\adminapiv3\service\platform_manage\ServerUpdateService;
use think\Request;

class ServerUpdateController extends BaseController
{
    /**
     * @name 产品升级
     * @author wx
     * @date 2019/11/6 16:50
     */
    public function upgrade(Request $request)
    {
        //记录操作日志
        global $logArray;
        $logArray = array(
            'content' => '产品升级',
        );
        //接收参数
        $params = $request->only([
            'ip','port'
        ],'post');

        $this->validate($params,'ServerUpdate.upgrade');
        $url = $_SERVER['REQUEST_SCHEME'].'://'.$params['ip'].':'.$params['port'].'/client/product/getInstallPackagePath';
        $server = new ServerUpdateService();
        $hostId = $server->toDo('upgrade',['url' => $url]);
        //记录操作日志
        $logArray['new_info'] = "IP地址：{$params['ip']}；端口号：{$params['port']}";

        $host_a = array();
        foreach ($hostId as $k => $v){
            $host_a[$k] = array('id' => $v);
        }

        //设置全局变量
        global $online_host;
        $online_host = $host_a;

        //下发给客户端
        $this->sendCommandToClient('client', 'productupgrade', false);
        return $this->jsonOutPut();
    }

    /**
     * @name 离线更新
     * @author wx
     * @date 2019/11/6 16:51
     */
    public function offline()
    {
        //接收参数
        $file = request()->file('file');

        $server = new ServerUpdateService();
        $server->offline($file);

        return $this->jsonOutPut();
    }

}